﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EvolutionBaseType;
using Evolution.Structure.Globe.Details.Cities;
using System.ComponentModel;



namespace Evolution.Structure.Globe.Fields
{
    public class Field : WorldItem, INotifyPropertyChanged
    {
        #region PROPERTIES
        public Resources HarvestableResources { get; set; }
        public bool InfluencedByCity
        {
            get { return _InfluencedByCity; }
            set
            {
                _InfluencedByCity = value;
                onPropertyChanged("InfluencedByCity");
            }
        }
        private bool _InfluencedByCity;

        public FieldType Type
        {
            get { return _Type; }
            set
            {
                _Type = value;
                manageHarvestableResources(value);
            }
        }
        private FieldType _Type;

        public City City
        {
            get { return _City; }
            set
            {
                _City = value;
                onPropertyChanged("City");
                onPropertyChanged("HasCity");
            }
        }
        private City _City;

        public Boolean HasCity
        {
            get
            {
                if (City != null) return true;
                return false;
            }
        }
        #endregion

        #region CONSTRUCTORS

        /// <summary>
        /// Generates field based on type
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pos"></param>
        public Field(FieldType type, Point pos)
        {
            this.HarvestableResources = new Resources();

            this.Type = type;
            this.HarvestableResources.ResearchPoints.Value = 0;
            this.HarvestableResources.CulturePoints.Value = 0;

            this.Position = pos;
        }

        /// <summary>
        /// Generates and emtpy default Field (BLACK)
        /// </summary>
        public Field()
        {
            this.HarvestableResources = new Resources();
            this.Type = FieldType.Empty;
            this.HarvestableResources.ResearchPoints.Value = 0;
            this.HarvestableResources.CulturePoints.Value = 0;            
        }

        #endregion

        private void manageHarvestableResources(FieldType type)
        {
            switch (type)
            {
                case FieldType.Water:
                    this.HarvestableResources.IndustryPoints.Value = 0;
                    this.HarvestableResources.FoodPoints.Value = 3;
                    this.HarvestableResources.MoneyPoints.Value = 3;
                    break;
                case FieldType.Forest:
                    this.HarvestableResources.IndustryPoints.Value = 2;
                    this.HarvestableResources.FoodPoints.Value = 2;
                    this.HarvestableResources.MoneyPoints.Value = 2;
                    break;
                case FieldType.Grassland:
                    this.HarvestableResources.IndustryPoints.Value = 1;
                    this.HarvestableResources.FoodPoints.Value = 3;
                    this.HarvestableResources.MoneyPoints.Value = 2;
                    break;
                case FieldType.Mountain:
                    this.HarvestableResources.IndustryPoints.Value = 6;
                    this.HarvestableResources.FoodPoints.Value = 0;
                    this.HarvestableResources.MoneyPoints.Value = 0;
                    break;
                case FieldType.Empty:
                    this.HarvestableResources.IndustryPoints.Value = 0;
                    this.HarvestableResources.FoodPoints.Value = 0;
                    this.HarvestableResources.MoneyPoints.Value = 0;
                    break;
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void onPropertyChanged(String PropertyName)
        {
            this.verifyPropertyName(PropertyName);

            PropertyChangedEventHandler handler = this.PropertyChanged;

            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(PropertyName);
                handler(this, e);
            }
        }


        public void verifyPropertyName(String PropertyName)
        {
            if (TypeDescriptor.GetProperties(this)[PropertyName] == null)
            {
                string msg = "Invalid Propertyname : " + PropertyName;
                throw new Exception(msg);
            }
        }
    }
}
